package com.smartEdu.service.impl.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.commons.enums.RespCodeEnum;
import com.smartEdu.mapper.common.ExamPaperMapper;
import com.smartEdu.mapper.common.HomeWorkReportMapper;
import com.smartEdu.model.homework.report.HworkReportModel;
import com.smartEdu.model.homework.report.QuestTypeModel;
import com.smartEdu.po.HomeWorkRecordPo;
import com.smartEdu.service.HomeWorkReportService;
import com.smartEdu.utils.JsonUtil;
import com.smartEdu.vo.web.TableResult;

@Service
public class HomeWorkReportServiceImpl implements HomeWorkReportService {
	@Autowired
	private ExamPaperMapper mapper;
	
	@Autowired
	private HomeWorkReportMapper reportMapper;
	
	public TableResult findStatistics(String id) {
		TableResult result = new TableResult();
		String questions = mapper.findQuestion(id);
		TreeMap<Integer, HworkReportModel> questMap = new TreeMap<Integer, HworkReportModel>();
		List<HworkReportModel> list = new ArrayList<>();
		int count = 0;
		try{
			int questCount = 1;
			if(questions != null) {
				List<QuestTypeModel> questionlist = JsonUtil.toListObject(questions, QuestTypeModel.class);
				for (QuestTypeModel questTypeModel : questionlist) {
					String questType = questTypeModel.getType();
					List<String> qidList = new ArrayList<>();
					for(String qid : questTypeModel.getList()) {
						qidList.add(qid);
					}
					List<HworkReportModel> reportList = mapper.findReport(Tag_Tips.QUESBANK + questType, qidList);
					if (reportList != null) {
						for(int a = 0;a < 3; a++) {
							
						for(int i = 0;i < reportList.size();i++) {
							questMap.put(questCount, reportList.get(i).clone());
							questCount++;
						}
						}
					}
					int tcount = mapper.findReportCount(Tag_Tips.QUESBANK + questType, qidList);
					count = count + tcount;
					count *=3;
				}
				result.setTotal(count);
				List<HomeWorkRecordPo> reportPolist = reportMapper.findStatistics(Tag_Tips.ACADEMICVALUE,id);
				Set<Entry<Integer, HworkReportModel>> mapentrys = questMap.entrySet();
				for (HomeWorkRecordPo homeWorkRecordPo : reportPolist) {
					if(homeWorkRecordPo.getAnswer() != null || !"".equals(homeWorkRecordPo.getAnswer())) {
						
					}
					Map<String, String> recordMap = JsonUtil.toMap(homeWorkRecordPo.getAnswer(), String.class);
					for(Entry<Integer, HworkReportModel> entry : mapentrys) {
						String value = null;
						if(recordMap!=null && recordMap.size()!=0) {
							value = recordMap.get(String.valueOf(entry.getKey()));
						}
						if(value != null) {
							if (entry.getValue().getAnswer().equals(value)) {
								entry.getValue().setRight(entry.getValue().getRight() + 1);
							}
							else {
								entry.getValue().setWrong(entry.getValue().getWrong() + 1);
							}
						}
						else {
							entry.getValue().setNodo(entry.getValue().getNodo() + 1);
						}
					}
				}
				
				for (Entry<Integer, HworkReportModel> entry : mapentrys) {
					entry.getValue().setId(entry.getKey());
					list.add(entry.getValue());
				}
			}
			result.setRows(list);
		} catch (Exception e) {
			result.setTotal(0);
			result.setCode(RespCodeEnum.ERROR.getCode());
			LogCvt.error(e.getMessage(), e);
		} finally {
			
			
		}
		LogCvt.info(JsonUtil.toJson(result));
		return result;
	}

	@Override
	public TableResult findDetaileReport(String id) {
		TableResult result = new TableResult();
		List<HomeWorkRecordPo> list = reportMapper.findDetaileReport(id);
		for (HomeWorkRecordPo homeWorkRecordPo : list) {
			
		}
		return null;
	}
	
}
